<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="input"
        @change="handle"
      ></el-input>
      <el-button @click="clear">清除</el-button>
      <el-button type="primary" @click.enter="search">搜索</el-button>
    </div>
    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="update(row.id)">编辑</el-button>
          <!-- 删除 -->
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="del(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <!-- 分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="currentChange"
        :page-size="params._limit"
      >
      </el-pagination>
      <el-button size="small" plain @click="add">+ 新增</el-button>
    </div>
    <!-- ------------新增/编辑弹框 -->
    <el-dialog title="弹框" :visible="dialogVisible" @close="closeDialog">
      <!-- 表单内容 -->
      <template #default>
        <!-- 进行表单验证，添加表单对象和规则对象，及双向绑定数据渲染 -->
        <el-form ref="Form" :model="Form" :rules="rules" label-width="100px">
          <el-form-item label="任务名称" prop="name">
            <el-input v-model="Form.name" />
          </el-form-item>
          <el-form-item label="任务描述" prop="des">
            <el-input v-model="Form.des" />
          </el-form-item>
        </el-form>
      </template>
      <!-- ------------底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="cancel">取消</el-button>
        <el-button size="small" type="primary" @click="Submit">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getAPI, delAPI, updateAPI, API, addAPI, searchAPI } from '@/api/index.js'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'MyApp',
  data() {
    return {
      list: [],
      params: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      dialogVisible: false,
      // 表单对象
      Form: {
        name: '',
        des: '',
        id: uuidv4()
      },
      // 规则
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        des: [{ required: true, message: '描述不能为空', trigger: 'blur' }]
      },
      total: 0,
      input: ''
    }
  },
  mounted() {
    this.getList()
    this.search()
  },
  methods: {
    async getList() {
      const res = await getAPI(this.params)
      this.list = res.data
      // 需要把total转成数字格式
      const total = Number(res.headers['x-total-count'])
      this.total = total
    },
    add() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    },
    cancel() {
      this.dialogVisible = false
      this.$refs.Form.resetFields()
      this.Form = {
        name: '',
        des: '',
        id: ''
      }
    },
    async del(id) {
      await delAPI(id)
      this.getList() // 更新列表
      this.$message.success('删除成功')
    },
    async update(id) {
      const res = await API(id)
      this.Form = res.data
      this.dialogVisible = true
    },
    Submit() {
      this.$refs.Form.validate(async valid => {
        if (valid) {
          if (this.Form.id) {
            // 调用api接口--编辑
            await updateAPI(this.Form)
          } else {
            // 调用api接口--新增
            await addAPI(this.Form)
          }
          this.$message.success(`${this.Form.id ? '编辑成功' : '新增成功'}`) // 提示
          this.dialogVisible = false // 关闭弹框
          this.getList()
          this.$refs.Form.resetFields() // 重置表单
          // 清空数据
          this.Form = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    },
    // 分页器
    currentChange(page) {
      this.params._page = page
      this.getList()
    },
    async search() {
      await searchAPI(this.input)
    },
    handle(val) {
      this.input = val
      // 过滤
      this.list = this.list.filter(item => item.name.includes(val))
    },
    clear() {
      this.input = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
